package com.atguigu.gmall.realtime.join;

import com.atguigu.gmall.realtime.app.BaseSQLApp;
import com.atguigu.gmall.realtime.util.SQLUtil;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lzc
 * @Date 2023/8/2 09:08
 */
public class LeftJoinToKafka extends BaseSQLApp {
    public static void main(String[] args) {
        new LeftJoinToKafka().start(
            5555,
            2,
            "LeftJoin"
        );
    }
    
    @Override
    public void handle(StreamExecutionEnvironment env,
                       StreamTableEnvironment tEnv) {
        //tEnv.getConfig().setIdleStateRetention(Duration.ofSeconds(10));
        
        tEnv.executeSql("create table t1(" +
                            " id string, " +
                            " name string " +
                            ")" + SQLUtil.getKafkaSourceSQL("InnerJoin1", "t1", "csv"));
    
        tEnv.executeSql("create table t2(" +
                            " id string, " +
                            " age int " +
                            ")" + SQLUtil.getKafkaSourceSQL("InnerJoin2", "t2", "csv"));
    
        Table result = tEnv.sqlQuery("select " +
                                        " t1.id, " +
                                        " name, " +
                                        " age " +
                                        "from t1 " +
                                        "left join t2 on t1.id=t2.id");
    
        tEnv.createTemporaryView("result", result);
    
        tEnv.executeSql("create table t12(" +
                            " id string , " +
                            " name string, " +
                            " age int, " +
                            " primary key(id) NOT ENFORCED" +
                            ")" + SQLUtil.getUpsertKafkaSQL("tt12", "json"));
    
        result.executeInsert("t12");
    
    
    }
}
/*
1. left join 的结果无法直接写入到普通的 kafka 中


 */